home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
boe.pres.k12.wv.us
/
boe.pres.k12.wv.us.zip
/
boe.pres.k12.wv.us
/
Utilities
/
Xerox Workcentre 5335
/
Windows Scan
/
64-bit_x64
/
Japanese
/
cpsimage.cab
/
data
/
xipProcs
/
colorMap.proc
next >
Wrap
Text File
|
2009-04-23
|
14KB
|
563 lines
// Load scripted support procedures
#load "xipProcs/printLayer.proc";
// Dynamically load necessary functions
LoadClasses (filename: "xeng");
CLASS XIPCOLORMAP {
LIST entries;
INTEGER nentries;
INTEGER photometry;
INTEGER colorseps;
BOOLEAN debug;
METHOD exec (XIPIMAGE input, STRING method, INTEGER maxcolors)
RETURNS (INTEGER ncolors)
{
STRING fn = "XIPCOLORMAP.exec";
XIPCOLOR color;
XIPIMAGE img;
LIST c;
STRING str;
STRING tmp;
INTEGER k, nseps;
INTEGER num;
INTEGER imgphoto;
INTEGER inphoto;
INTEGER skipOne = 0;
INTEGER cMaxEntries = 256;
if (this.debug)
print "Executing XIPCOLORMAP.exec";
// Check for full colormap
if (this.nentries == cMaxEntries) {
print fn + ": Colormap contains maximum number of entries";
return;
}
// Check for invalid requested number of colors
if ((maxcolors < 1) || (maxcolors > cMaxEntries)) {
print fn + ": Requested color count must between 1 and 256 inclusive";
return;
}
// Adjust requested number of colors if not enough space
if ((this.nentries + maxcolors) > cMaxEntries)
maxcolors = cMaxEntries - this.nentries;
// Check photometry
imgphoto = input.getMember (member: "photometry");
if (this.photometry == XIP_UNKNOWN_COLOR) {
this.photometry = imgphoto;
this.colorseps = input.getMember (member: "seps");
}
if ((method == "simple") ||
(this.photometry == XIP_XRGB_COLOR) ||
(this.photometry == XIP_SRGB_COLOR) ||
(this.photometry == XIP_GENRGB_COLOR)) {
inphoto = this.photometry;
} else {
if ((imgphoto == XIP_XRGB_COLOR) ||
(imgphoto == XIP_SRGB_COLOR) ||
(imgphoto == XIP_GENRGB_COLOR))
inphoto = imgphoto;
else
inphoto = XIP_SRGB_COLOR;
}
if (this.debug) {
print " imgphoto: " + Photo2Str (photo: imgphoto);
print " inphoto: " + Photo2Str (photo: inphoto);
}
img = input;
if (inphoto != imgphoto)
img = img.cspace (outspace: Photo2Str (photo: inphoto), precise: 1);
// Check interleave
if (img.getMember (member: "interleave") != XIP_SCANLINE_INTER)
img = img.interleave (scanline: 1);
// Check bits per pixel
if (img.getMember (member: "bits") != 8)
img = img.convert (to: (8));
// Execute mapping operation
if (method == "simple") {
str = img.simplemap (entries: maxcolors
).toString();
} else if (method == "diverse") {
str = img.diversemap (entries: maxcolors
).toString();
} else if (method == "heckbert") {
str = img.heckbertmap (entries: maxcolors
).toString();
} else if (method == "octree") {
str = img.octreemap (entries: maxcolors
).toString();
} else {
print fn + ": Unknown mapping method: " + method;
return;
}
nseps = GetImageSeps (photo: inphoto);
ncolors = 0;
for (tmp=str.getToken (); tmp; tmp=str.getToken ())
if (tmp == "color:") {
if (!skipOne) {
str.getLine ();
skipOne++;
} else {
color = new (XIPCOLOR);
color.photometry = inphoto;
c = new (LIST);
for (k=0; k<nseps; k++)
c.insert (obj: new (INTEGER, value:0));
tmp = str.getLine ();
for (k=0; k<nseps; k++) {
num = tmp.getToken ();
c[k] = num;
}
color.c = c;
color = XIPColorChangespace (incolor: color, photo: this.photometry);
if (!this.contains (color: color)) {
this.entries.insert (obj: color, entry: this.nentries);
this.nentries++;
ncolors++;
}
}
}
if (this.photometry != inphoto)
this.sort ();
}
METHOD apply (XIPIMAGE input)
RETURNS (XIPIMAGE output)
{
XIPIMAGE img = input;
INTEGER inphoto;
INTEGER origphoto;
INTEGER imgphoto;
// Check interleave
if (img.getMember (member: "interleave") != XIP_SCANLINE_INTER)
img = img.interleave (scanline: 1);
// Check bits per pixel
if (img.getMember (member: "bits") != 8)
img = img.convert (to: (8));
// Get processing photometry
imgphoto = img.getMember (member: "photometry");
if ((imgphoto == XIP_XRGB_COLOR) ||
(imgphoto == XIP_SRGB_COLOR) ||
(imgphoto == XIP_GENRGB_COLOR)) {
inphoto = imgphoto;
} else if ((this.photometry == XIP_XRGB_COLOR) ||
(this.photometry == XIP_SRGB_COLOR) ||
(this.photometry == XIP_GENRGB_COLOR)) {
inphoto = this.photometry;
} else {
inphoto = XIP_SRGB_COLOR;
}
if (this.photometry == inphoto) {
this.writemap (filename: "cmap");
} else {
origphoto = this.photometry;
this.changespace (photo: inphoto);
this.sort ();
this.writemap (filename: "cmap");
this.changespace (photo: origphoto);
this.sort ();
}
if (imgphoto == inphoto)
output = img.tomap (cmap: "cmap"
).unmap (
).exec (
);
else
output = img.cspace (outspace: Photo2Str (photo: inphoto), precise: 1
).tomap (cmap: "cmap"
).cspace (outspace: Photo2Str (photo: imgphoto), precise: 1
).exec (
);
System (cmd: "rm -f cmap");
}
METHOD add (XIPCOLOR incolor)
{
STRING fn = "XIPCOLORMAP.add";
XIPCOLOR color;
// Check input color photometry
if (incolor.photometry == XIP_UNKNOWN_COLOR) {
print fn + ": Input color photometry is unknown";
return;
}
// Check colormap photometry
if (this.photometry == XIP_UNKNOWN_COLOR) {
this.photometry = incolor.photometry;
this.colorseps = GetImageSeps (photo: incolor.photometry);
}
color = XIPColorChangespace (incolor: incolor, photo: this.photometry);
if (!this.contains (color: color)) {
this.entries.insert (obj: color, entry: this.nentries);
this.nentries++;
}
}
METHOD addList (LIST colors)
{
INTEGER i;
for (i=0; i<colors.length (); i++)
this.add (incolor: colors[i]);
}
METHOD remove (XIPCOLOR incolor)
{
STRING fn = "XIPCOLORMAP.remove";
XIPCOLOR color;
INTEGER idx;
// Check input color photometry
if (incolor.photometry == XIP_UNKNOWN_COLOR) {
print fn + ": Input color photometry is unknown";
return;
}
// Check colormap photometry
if (this.photometry == XIP_UNKNOWN_COLOR) {
print fn + ": Colormap photometry is unknown";
return;
}
color = XIPColorChangespace (incolor: incolor, photo: this.photometry);
idx = this.find (color: color);
if (idx >= 0) {
this.entries.remove (entry: idx);
this.nentries--;
}
}
METHOD clear ()
{
if (this.entries.length ())
this.entries = new (LIST);
this.photometry = XIP_UNKNOWN_COLOR;
this.colorseps = 0;
this.nentries = 0;
}
METHOD find (XIPCOLOR color)
RETURNS (INTEGER entry)
{
XIPCOLOR mapcolor;
INTEGER i, k, found;
entry = -1;
if (this.nentries == 0) {
return;
}
if (color.photometry != this.photometry) {
return;
}
for (i=0; i<this.nentries; i++) {
mapcolor = this.entries[i];
if (XIPColorCompare (lhs: mapcolor, rhs: color) == 0) {
found = 0;
break;
}
}
if (found) {
entry = i;
break;
}
}
METHOD contains (XIPCOLOR color)
RETURNS (INTEGER found)
{
if (this.find (color: color) == -1)
found = 0;
else
found = 1;
}
METHOD sort ()
{
XIPCOLOR color;
LIST c;
INTEGER done, ret;
INTEGER l, j, ir, i, k;
for (k=0; k<this.colorseps; k++)
c.insert (obj: new (INTEGER, value:0));
color.c = c;
color.photometry = this.photometry;
l = this.nentries / 2;
ir = this.nentries - 1;
if (ir <= 0)
return;
for (done=0; !done;) {
if (l > 0) {
l--;
color.c = this.entries[l].c;
} else {
color.c = this.entries[ir].c;
this.entries[ir].c = this.entries[0].c;
ir--;
if (ir == 0) {
this.entries[0].c = color.c;
done = 1;
continue;
}
}
i = l;
for (j=l+1; j<=ir; ) {
if (j < ir) {
ret = XIPColorCompare (lhs: this.entries[j], rhs: this.entries[j+1]);
if (ret < 0)
j++;
}
if (XIPColorCompare (lhs: color, rhs: this.entries[j]) < 0) {
this.entries[i].c = this.entries[j].c;
i = j;
j = j + i;
} else {
j = ir + 1;
}
}
this.entries[i].c = color.c;
}
}
METHOD changespace (INTEGER photo)
{
STRING fn = "XIPCOLORMAP.changespace";
XIPCOLOR color;
INTEGER i, k, nseps;
STATUS status;
if ((photo == XIP_UNKNOWN_COLOR) || (photo == XIP_MULTIPART_COLOR)) {
print fn + ": Cannot change colormap photometry to " +
Photo2Str (photo: photo) + " photometry";
return;
}
if (photo == this.photometry)
return;
try {
nseps = GetImageSeps (photo: photo);
for (i=0; i<this.nentries; i++) {
color = XIPColorChangespace (incolor: this.entries[i], photo: photo);
this.entries[i] = color;
}
this.photometry = photo;
this.colorseps = nseps;
} catch {
print "WTF";
print status.message;
end;
}
}
METHOD reduce ()
{
XIPCOLOR clr;
LIST colorclass, colors, l;
STRING colorinfo;
INTEGER i, k, classid;
if (this.debug)
print "Reducing XIPCOLORMAP:";
this.sort ();
for (i=0; i<15; i++)
colorclass.insert (obj: new (LIST));
if (this.debug)
print "Colormap Pre-Reduction:";
for (i=0; i<this.nentries; i++) {
clr = this.entries[i];
classid = XIPColorClass (color: clr);
if (this.debug)
this.printEntryAttributes (entry: i);
colorclass[classid].insert (obj: clr);
}
for (i=0; i<15; i++) {
if (colorclass[i].length () > 0) {
k = colorclass[i].length () / 2;
l = colorclass[i];
colors.insert (obj: l[k], entry: colors.length ());
}
}
this.nentries = colors.length ();
this.entries = colors;
if (this.debug) {
print "Colormap Post-Reduction:";
for (i=0; i<this.nentries; i++)
this.printEntryAttributes (entry: i);
}
}
METHOD threshold (XIPCOLOR mincolor, XIPCOLOR maxcolor)
{
XIPCOLOR min, max, mapcolor;
LIST colors;
INTEGER i, k, inrange;
min = XIPColorChangespace (incolor: mincolor, photo: this.photometry);
max = XIPColorChangespace (incolor: maxcolor, photo: this.photometry);
for (i=0; i<this.nentries; i++) {
mapcolor = this.entries[i];
inrange = 1;
for (k=0; k<this.colorseps; k++) {
if ((mapcolor.c[k] < min.c[k]) || (mapcolor.c[k] > max.c[k])) {
inrange = 0;
break;
}
}
if (inrange)
colors.insert (obj: mapcolor, entry: colors.length ());
}
this.nentries = colors.length ();
this.entries = colors;
}
METHOD readmap (STRING filename, INTEGER photo)
{
STRING fn = "XIPCOLORMAP.readmap";
XIPCOLOR color;
LIST c;
STRING str, tmp, val;
INTEGER num;
if (!IsFile (filename: filename)) {
print fn + ": No such file " + filename;
return;
}
str = ReadObject (filename: filename, override: 1);
if (photo == XIP_UNKNOWN_COLOR) {
photo = XIP_SRGB_COLOR;
} else if (photo == XIP_MULTIPART_COLOR) {
print fn + ": Colormap cannot have multipart photometry";
return;
}
this.photometry = photo;
this.colorseps = GetImageSeps (photo: photo);
for (tmp=str.getLine (); tmp; tmp=str.getLine ()) {
c = new (LIST);
for (val=tmp.getToken (); val; val=tmp.getToken ()) {
num = val;
c.insert (obj: new (INTEGER, value:num), entry: c.length ());
}
color = new (XIPCOLOR);
color.photometry = photo;
color.c = c;
if (!this.contains (color: color)) {
this.entries.insert (obj: color, entry: this.nentries);
this.nentries++;
}
}
}
METHOD writemap (STRING filename)
{
XIPCOLOR color;
STRING str;
INTEGER i, k;
if (filename) {
// Remove existing colormap file
System (cmd: "rm -f " + filename);
// Write colormap values to file
for (i=0; i<this.nentries; i++) {
color = this.entries[i];
str = color.c[0];
for (k=1; k<this.colorseps; k++)
str = str + " " + color.c[k];
str = str + "\n";
str.Write (filename: filename, mode: "a", value: 1);
}
} else {
// Write colormap values to stderr
for (i=0; i<this.nentries; i++) {
color = this.entries[i];
str = color.c[0];
for (k=1; k<this.colorseps; k++)
str = str + " " + color.c[k];
str = str + "\n";
str.Write (fd: 2, value: 1);
}
}
}
METHOD printEntryAttributes (INTEGER entry)
{
XIPCOLOR clr;
STRING clrinfo;
INTEGER k;
if ((entry < 0) || (entry >= this.nentries)) {
print "Map does not contain entry at index " + entry;
return;
}
clr = this.entries[entry];
clrinfo = " entry " + entry + ": \n values = (" + clr.c[0];
for (k=1; k<this.colorseps; k++)
clrinfo = clrinfo + ", " + clr.c[k];
clrinfo = clrinfo + ")\n name = \""
+ XIPColorName (color: clr) + "\"";
print clrinfo;
}
}